package spikes

import (
	"context"
	"gitee.com/enjoy_5/spikes-rpc/internal/server"
	pb "gitee.com/enjoy_5/spikes-rpc/proto/spikes"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

// 创建订单
func (u *SpikesServer) CreateSpikesOrder(_ context.Context, req *pb.CreateSpikesOrderRequest) (resp *pb.CreateSpikesOrderResponse, err error) {
	if req.UserID == 0 {
		return nil, status.Errorf(codes.NotFound, "用户id不能为空")
	}
	if req.SpikesActivityID == 0 {
		return nil, status.Errorf(codes.NotFound, "秒杀活动id不能为空")
	}
	if req.GoodsID == 0 {
		return nil, status.Errorf(codes.NotFound, "商品id不能为空")
	}
	if req.Num == 0 {
		return nil, status.Errorf(codes.NotFound, "购买数量不能为0")
	}
	if req.PaymentMethod == 0 {
		return nil, status.Errorf(codes.NotFound, "支付方式不能为空")
	}

	orders, err := server.CreateSpikesOrders(req)
	if err != nil {
		return nil, status.Errorf(codes.NotFound, "创建订单失败::%s", err)
	}
	return &pb.CreateSpikesOrderResponse{In: orders}, nil
}

// 删除订单
func (u *SpikesServer) DeleteSpikesOrder(_ context.Context, req *pb.DeleteSpikesOrderRequest) (resp *pb.DeleteSpikesOrderResponse, err error) {
	if req.UserID == 0 {
		return nil, status.Errorf(codes.NotFound, "用户id不能为空")
	}
	if req.OrderID == 0 {
		return nil, status.Errorf(codes.NotFound, "订单id不能为空")
	}

	err = server.DeleteOrder(req)
	if err != nil {
		return nil, status.Errorf(codes.NotFound, "删除订单失败::%s", err)
	}
	return resp, nil
}
